<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$error = FALSE;
$location_id = $_GET['id'];
$location = $locationMgr->get($location_id);
if(!$location) {
    header('Location: /notfound.php');
    exit();
}

if(isset($_SESSION['msg']) && $_SESSION['msg']) {
   $error = $_SESSION['msg'];
   unset($_SESSION['msg']);
}

$route_count = $locationMgr->getRouteCount($location_id);
$route_total = $locationMgr->getUserLinkCount($location_id);
$author_count = $locationMgr->getAuthorCount($location_id);
$user_count = $locationMgr->getUserCount($location_id);
$newest_route = $locationMgr->getNewestRoute($location_id);

$newest_author_info = ($newest_route['author_id'] ? '<b><a href="/viewauthor.php?id=' . $newest_route['author_id'] . '">' . $newest_route['author'] . '</a></b>' : ($newest_route['author'] ? '<b>' . $newest_route['author'] . '</b>' : '(empty)'));
$newest_created_info = ($newest_route['created'] ? '<b>' . date('d-m-Y', strtotime($newest_route['created'])) . '</b>' : '(empty)');
$replaces = array(
    'id'                        => $location_id,
    'name'                      => $location['name'],
    'description'               => $location['description'],
    'author_count'              => $author_count,
    'route_count'               => $route_count,
    'route_total'               => $route_total,
    'user_count'                => $user_count,
    'newest_id'                 => $newest_route['id'],
    'newest_colour'             => $newest_route['colour'],
    'newest_difficulty'         => $newest_route['difficulty'],
    'newest_name'               => $newest_route['name'],
    'is_short'                  => ($newest_route['is_short'] ? '(short)' : ''),
    'newest_author_info'        => ($newest_route['author_id'] ? '<b><a href="/viewauthor.php?id=' . $newest_route['author_id'] . '">' . $newest_route['author'] . '</a></b>' : ($newest_route['author'] ? '<b>' . $newest_route['author'] . '</b>' : '(empty)')),
    'newest_created_info'       => ($newest_route['created'] ? '<b>' . date('d-m-Y', strtotime($newest_route['created'])) . '</b>' : '(empty)'),
    'admin_info'                => (in_array($_SESSION['user']['id'], $admin_users) ? '<p><a href="/editlocation.php?id=' . $location_id . '">' . $langMgr->get('text_location_edit') . '</a></p>' : ''),
    'error'                     => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_statistics'             => $langMgr->get('header_statistics'),
    'text_location_routecount'      => $langMgr->get('text_location_routecount', false, array('routecount' => $route_count, 'authorcount' => $author_count)),
    'text_location_user_stats'      => $langMgr->get('text_location_user_stats', false, array('user_count' => $user_count, 'route_total' => $route_total)),
    'text_location_newest_route'    => $langMgr->get('text_location_newest_route'),
    'text_location_newest_created'  => $langMgr->get('text_location_newest_created', false, array('newest_author_info' => $newest_author_info, 'newest_created_info' => $newest_created_info)),
    'header_location_diff_range'    => $langMgr->get('header_location_diff_range'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
